package com.example.service.Impl;

import com.example.exception.CustomException;
import com.example.mapper.RoomImageMapper;
import com.example.mapper.RoomMapper;
import com.example.mapper.RoomTypeMapper;
import com.example.pojo.Room;
import com.example.pojo.RoomImage;
import com.example.pojo.RoomType;
import com.example.service.RoomService;
import com.example.service.RoomTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoomTypeServiceImpl implements RoomTypeService {
    @Resource
    private RoomTypeMapper roomTypeMapper;

    @Resource
    private RoomMapper roomMapper;


    /**
     * 添加房间分类
     *
     * @param roomType
     */
    @Override
    public void add(RoomType roomType) {
        RoomType dbRoomType = roomTypeMapper.selectByname(roomType.getName());
        if (dbRoomType!=null){
            throw new CustomException("500","房间分类已存在");
        }
        roomType.setnum(0);//新的房间分类的房间数量为0
       roomTypeMapper.add(roomType);

        }

    /**
     * 查询所有房间分类
     *
     * @return
     */
    @Override
    public List<RoomType> selectAll(RoomType roomType) {
        List<RoomType> list = roomTypeMapper.selectAll(roomType);
        // 更新每个分类的房间数量
        for (RoomType type : list) {
            int count = roomMapper.countByTypeId(type.getId());
            type.setnum(count);
        }
        return list;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public RoomType selectById(Integer id) {
        RoomType roomType = roomTypeMapper.selectById(id);
        return roomType;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        roomTypeMapper.deleteById(id);
    }

    /**
     * 批量删除数据
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id:ids){
            roomTypeMapper.deleteById(id);
        }

    }

    /**
     * 修改房间分类
     *
     * @param roomType
     */
    @Override
    public void updateById(RoomType roomType) {
        roomTypeMapper.updateById(roomType);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<RoomType> selectPage(RoomType roomType, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<RoomType> list = roomTypeMapper.selectAll(roomType);
        // 更新每个分类的房间数量
        for (RoomType type : list) {
            int count = roomMapper.countByTypeId(type.getId());
            type.setnum(count);
            roomTypeMapper.updateRoomCount(type.getId(),count);
        }
        return PageInfo.of(list);
    }
}
